package com.myecl.poker.state;

import java.util.LinkedList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

import com.myecl.poker.communication.CommandHandler;
import com.myecl.poker.option.Command;
import com.myecl.poker.rules.Rules;



public class Game {	
	private Rules rules;
	private Round round;
	private CommandHandler commandHandler = new CommandHandler();
	private Player button;
	private List<Player> players = new LinkedList<Player>();
	
	private final Logger logger = LogManager.getLogger("com.myecl.poker.state.Game");
	private final Marker commandMarker = MarkerManager.getMarker("COMMAND");

	
	public Game(Rules rules) {	
		this.rules = rules;
	}
	
	public void newRound() {
		assert players.size() > 1;
		if (button == null) {
			button = players.get(0);
		}
		round = new Round(players, rules.getDeck().getCards(), button);
		logger.trace(commandMarker, "First Player: " + round.nextPlayer().getName());
	}
	
	public void newCommand(Player player, Command command) {
		commandHandler.addCommand(player, command);
		if (player.equals(round.nextPlayer())) {
			round.execute(commandHandler.getCommands(round.nextPlayer()));
		}
		logger.trace(commandMarker, "Next Player:" + round.nextPlayer().getName());
	}
	
	public void addPlayer(Player player) {
		players.add(player);
	}
	
	public List<Player> getPlayers() {
		return players;
	}
	
	public void removePlayer(Player player) {
		assert players.contains(player); 
		players.remove(player);
	}
	
	public Round getRound() {
		return round;
	}
}
